package org.common.utils.generatorentity.event.handler.service;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.common.utils.field.FieldInfo;
import org.common.utils.field.GeneratorConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * @author zhouzhibing
 * @date 2025/8/5
 */
public class GeneratorService {
    private static final Logger log = LoggerFactory.getLogger(GeneratorService.class);

    public static final GeneratorService INSTANCE = new GeneratorService();

    private GeneratorService() {
        initVelocity();
    }

    public static GeneratorService getInstance() {
        return INSTANCE;
    }

    private void initVelocity() {
        Properties props = new Properties();
        props.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        props.put("runtime.references.strict", "false");
        Velocity.init(props);
    }

    public void generator(GeneratorConfig config) {
        generatorAbstractEntity(config);
        generatorEntity(config);
    }

    /**
     * 生成实体类
     * @param config 生成配置
     */
    private void generatorEntity(GeneratorConfig config) {
        String className = config.getClassName();
        String packageName = config.getPackageName();
        List<FieldInfo> fieldList = config.getFieldList();

        String fileName = className + ".java";
        String filePath = config.getAllPath() + fileName;
        File dataFile = new File(filePath);
        if(dataFile.exists()) {
            log.info("文件已存在，不进行覆盖生成！ filePath = {} " , filePath);
            return;
        }

        VelocityContext context = new VelocityContext();
        context.put("fileName", fileName);
        context.put("className", className);
        context.put("packageName", packageName);
        context.put("fieldList", fieldList);

        try (FileWriter writer = new FileWriter(filePath)) {
            Template template = Velocity.getTemplate("velocity/entity.vm", "UTF-8");
            template.merge(context , writer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 生成抽象实体类
     * @param config 生成配置
     */
    private void generatorAbstractEntity(GeneratorConfig config) {
        String className = config.getClassName();
        String packageName = config.getPackageName();
        List<FieldInfo> fieldList = config.getFieldList();

        className = "Abstract" + className;
        String fileName = className + ".java";
        String filePath = config.getAllPath() + fileName;

        VelocityContext context = new VelocityContext();
        context.put("fileName", fileName);

        context.put("className", className);
        context.put("packageName", packageName);
        context.put("fieldList", fieldList);

        try (FileWriter writer = new FileWriter(filePath)) {
            Template template = Velocity.getTemplate("velocity/abstract_entity.vm", "UTF-8");
            template.merge(context , writer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
